var express = require('express');
var router = express.Router();
const {login2,getadmin2,addadmin2,changeadmin2,adminone2,changestate2}=require("../service/admin.js")
//登录
router.post('/login',async function(req,res){
  const loginData=req.body;
   const result= await login2(loginData);
   console.log(result);
  if(result.length>0){
    if(result[0].loginState!=0){
      req.session.regenerate(function () {
        req.session.loginUser=result[0].userName;
        req.session.loginState=result[0].loginState;
        res.send(true)
      })
    }else{
      res.send(false)
    }
  }else{
    res.send(false)
  }
});
//查看登录状态
router.get('/isLogin', (req, res) => {
	const admins = req.session.loginUser;
	if (admins) {
		res.send({ isLogin: true, adminstate: req.session.loginState,admins });
	} else {
		res.send({ isLogin: false });
	}
})
//退出登录
router.get('/logout', (req, res) => {
	req.session.destroy(function() {
		//req.session.loginUser = null;
		res.clearCookie('admins');
		res.send(true);
	})
})
//获取用户列表
router.get('/getadmin',async function(req,res){
  const data=req.query;
  const result=await getadmin2(data);
  res.send(result);
})
//新增用户
router.post('/addadmin',async function(req,res){
  const data=req.body;
  const result=await addadmin2(data);
  res.send(result);
})
//找到修改用户
router.get('/adminone',async function(req,res){
  const data=req.query;
  const result=await adminone2(data);
  res.send(result)
})
//修改用户状态
router.post('/changeadmin',async function(req,res){
  const data=req.body;
  const result=await changeadmin2(data);
  res.send(result);
})
//修改用户信息
router.post('/changestate',async function(req,res){
  const data=req.body;
  const result=await changestate2(data);
  res.send(result);
})


module.exports = router;